local m = {}

-- Function to check if tournament ID is '0'
local function is_tournament_id_zero(ctx)
    return ctx.tournament_id == 0
end

function m.rewrite(ctx, filename)
    
    -- Replace specific files when tournament ID is '0'
    if is_tournament_id_zero(ctx) then
        if filename:match("ent_007_passage%02d_cmn_cam.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_007_passage%02d_cmn_cam.fdc", "ent_007_passage02_cmn_came.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename
        elseif filename:match("ent_007_passage%02d_cmn_cam_g3.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_007_passage%02d_cmn_cam_g3.fdc", "ent_007_passage02_cmn_came.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename
        elseif filename:match("ent_007_passage%02d_cmn_pl.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_007_passage%02d_cmn_pl.fdc", "ent_007_passage02_cmn_ple.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename
        elseif filename:match("ent_007_passage%02d_cmn_judge.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_007_passage%02d_cmn_judge.fdc", "ent_007_passage02_cmn_judgee.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename
        elseif filename:match("ent_007_passage03_cmn_cam.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_007_passage03_cmn_cam.fdc", "ent_007_passage02_cmn_came.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename
        elseif filename:match("ent_007_passage03_cmn_cam_g3.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_007_passage03_cmn_cam_g3.fdc", "ent_007_passage02_cmn_came.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename
        elseif filename:match("ent_007_passage03_cmn_pl.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_007_passage03_cmn_pl.fdc", "ent_007_passage02_cmn_judgee.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename
        elseif filename:match("ent_007_passage03_cmn_judge.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_007_passage03_cmn_judge.fdc", "ent_007_passage02_cmn_judgee.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename
        elseif filename:match("ent_007_passage%02d_obj.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_007_passage%02d_obj.fdc", "ent_007_passage02_obje.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename
        elseif filename:match("ent_007_st%d%d%d_cmn_pl.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_007_st%d%d%d_cmn_pl.fdc", "ent_007_passage02_cmn_ple.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename
        elseif filename:match("ent_007_st%d%d%d_cmn_cam.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_007_st%d%d%d_cmn_cam.fdc", "ent_007_passage02_cmn_came.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename
        elseif filename:match("ent_007_st%d%d%d_cmn_cam_g3.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_007_st%d%d%d_cmn_cam_g3.fdc", "ent_007_passage02_cmn_came.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename			
        elseif filename:match("ent_007_st%d%d%d_cmn_judge.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_007_st%d%d%d_cmn_judge.fdc", "ent_007_passage02_cmn_judgee.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename	
        elseif filename:match("ent_007_st%d%d%d_obj.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_007_st%d%d%d_obj.fdc", "ent_007_passage02_obje.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename				
        elseif filename:match("ent_009_st%d%d%d_cmn_cam.fdc") then
            log("Original CAM filename: " .. filename)
            local new_filename = filename:gsub("ent_009_st%d%d%d_cmn_cam.fdc", "ent_009_st029_cmn_came.fdc")
            log("Rewritten CAM filename: " .. new_filename)
            return new_filename
        elseif filename:match("ent_009_st%d%d%d_cmn_pl.fdc") then
            log("Original CMN_PL filename: " .. filename)
            local new_filename = filename:gsub("ent_009_st%d%d%d_cmn_pl.fdc", "ent_009_st029_cmn_ple.fdc")
            log("Rewritten CMN_PL filename: " .. new_filename)
            return new_filename
        elseif filename:match("ent_012_st%d%d%d_cmn_cam.fdc") then
            log("Original CAM filename: " .. filename)
            local new_filename = filename:gsub("ent_012_st%d%d%d_cmn_cam.fdc", "ent_012_st000_cmn_came.fdc")
            log("Rewritten CAM filename: " .. new_filename)
            return new_filename
        elseif filename:match("ent_012_st%d%d%d_cmn_national_cam.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_012_st%d%d%d_cmn_national_cam.fdc", "ent_012_st000_cmn_came.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename
        elseif filename:match("ent_012_st%d%d%d_cmn_national_cam_180.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_012_st%d%d%d_cmn_national_cam_180.fdc", "ent_012_st000_cmn_came.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename
        elseif filename:match("ent_012_st%d%d%d_cmn_pl.fdc") then
            log("Original CMN_PL filename: " .. filename)
            local new_filename = filename:gsub("ent_012_st%d%d%d_cmn_pl.fdc", "ent_012_st000_cmn_ple.fdc")
            log("Rewritten CMN_PL filename: " .. new_filename)
            return new_filename
        elseif filename:match("ent_016_cam_bkh_a00short_st000.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_016_cam_bkh_a00short_st000.fdc", "ent_016_cam_bkh_a00short_st000e.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename
        elseif filename:match("ent_016_cam_sho_h00short_st000.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_016_cam_sho_h00short_st000.fdc", "ent_016_cam_sho_h00short_st000e.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename
        elseif filename:match("ent_016_cam_sho_h00short_st000_180.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_016_cam_sho_h00short_st000_180.fdc", "ent_016_cam_sho_h00short_st000_180e.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename
        elseif filename:match("ent_016_cam_bkh_a00short_st000_180.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_016_cam_bkh_a00short_st000_180.fdc", "ent_016_cam_bkh_a00short_st000_180e.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename
        elseif filename:match("ent_016_cam_sho_h00short_st000_180.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_016_cam_sho_h00short_st000_180.fdc", "ent_016_cam_sho_h00short_st000_180e.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename 
        elseif filename:match("ent_016_cam_sho_h00short_st000.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_016_cam_sho_h00short_st000.fdc", "ent_016_cam_sho_h00short_st000e.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename 
        elseif filename:match("ent_016_club_away_st000_cam.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_016_club_away_st000_cam.fdc", "ent_016_club_away_st000_came.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename 
        elseif filename:match("ent_016_club_home_st000_cam.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_016_club_home_st000_cam.fdc", "ent_016_club_home_st000_came.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename 
        elseif filename:match("ent_016_club_away_st000_cam_180.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_016_club_away_st000_cam_180.fdc", "ent_016_club_away_st000_cam_180e.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename 
        elseif filename:match("ent_016_club_home_st000_cam_180.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_016_club_home_st000_cam_180.fdc", "ent_016_club_home_st000_cam_180e.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename 
        elseif filename:match("ent_016_pl_bth_a00_st000_on.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_016_pl_bth_a00_st000_on.fdc", "ent_016_pl_bth_a00_st000_one.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename 
        elseif filename:match("ent_016_pl_club_away_st000.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_016_pl_club_away_st000.fdc", "ent_016_pl_club_away_st000e.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename 
        elseif filename:match("ent_016_pl_club_home_st000.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_016_pl_club_home_st000.fdc", "ent_016_pl_club_home_st000e.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename 
        elseif filename:match("ent_016_pl_sho_h00_st000_on.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_016_pl_sho_h00_st000_on.fdc", "ent_016_pl_sho_h00_st000_one.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename 
        elseif filename:match("ent_018_cmn_all_pl.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_018_cmn_all_pl.fdc", "ent_018_cmn_all_ple.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename 
        elseif filename:match("ent_018_cmn_all_st000_cam.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_018_cmn_all_st000_cam.fdc", "ent_018_cmn_all_st000_came.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename 
        elseif filename:match("ent_018_cmn_all_st000_cam_180.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_018_cmn_all_st000_cam_180.fdc", "ent_018_cmn_all_st000_cam_180e.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename 
        elseif filename:match("ent_019_away_pl.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_019_away_pl.fdc", "ent_019_away_ple.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename 
        elseif filename:match("ent_019_away_st000_cam.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_019_away_st000_cam.fdc", "ent_019_away_st000_came.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename 
        elseif filename:match("ent_019_away_st000_cam_180.fdc") then
            log("Original filename: " .. filename) 
            local new_filename = filename:gsub("ent_019_away_st000_cam_180.fdc", "ent_019_away_st000_cam_180e.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename
        elseif filename:match("ent_019_away_st000_cam_180_g3.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_019_away_st000_cam_180_g3.fdc", "ent_019_away_st000_cam_180_g3e.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename
        elseif filename:match("ent_019_away_st000_cam_g3.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_019_away_st000_cam_g3.fdc", "ent_019_away_st000_cam_g3e.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename
        elseif filename:match("ent_020_order01_pl.fdc") then
            log("Original filename: " .. filename)
            local new_filename = filename:gsub("ent_020_order01_pl.fdc", "ent_020_order01_ple.fdc")
            log("Rewritten filename: " .. new_filename)
            return new_filename       
        end
    end

    -- Only process specific patterns to avoid unnecessary logging
    if filename:match("ent_007_st%d%d%d_obj.fdc") then
        log("Original CAM filename: " .. filename)
        local new_filename = filename:gsub("ent_007_st%d%d%d_obj.fdc", "ent_007_passage02_obje")
        log("Rewritten CAM filename: " .. new_filename)
        return new_filename
    elseif filename:match("ent_007_st%d%d%d_cmn_cam.fdc") then
        log("Original CMN_PL filename: " .. filename)
        local new_filename = filename:gsub("ent_007_st%d%d%d_cmn_cam.fdc", "ent_007_passage02_cmn_came.fdc")
        log("Rewritten CMN_PL filename: " .. new_filename)
        return new_filename
		elseif filename:match("ent_007_st%d%d%d_cmn_pl.fdc") then
        log("Original CMN_PL filename: " .. filename)
        local new_filename = filename:gsub("ent_007_st%d%d%d_cmn_pl.fdc", "ent_007_passage02_cmn_ple.fdc")
        log("Rewritten CMN_PL filename: " .. new_filename)
        return new_filename
		elseif filename:match("ent_007_st%d%d%d_cmn_judge.fdc") then
        log("Original CMN_PL filename: " .. filename)
        local new_filename = filename:gsub("ent_007_st%d%d%d_cmn_judge.fdc", "ent_007_passage02_cmn_judgee.fdc")
        log("Rewritten CMN_PL filename: " .. new_filename)
        return new_filename
    end

    -- Return the original filename if no match is found
    return filename
end

function m.init(ctx)
    ctx.register("livecpk_rewrite", m.rewrite)
end

return m